﻿using CHSystemApi.Business;
using CHSystemApi.Framework.AuthServices;
using CHSystemApi.Framework.Base;
using CHSystemApi.DataAccess.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security;

namespace CHSystemApi.Base.Controllers
{
    public class UserController : BaseController
    {
        public IUserBusiness _business;

        private IPermissionService _permissionService;

        public UserController(IUserBusiness business, IPermissionService permissionService)
        {
            _business = business;
            _permissionService = permissionService;
        }

        [HttpPost("/openapi/v1/1.0.1/user/login")]
        public BaseResponse Login([FromBody] LoginDtoIn dto)
        {
            dto.ip = HttpContext.Request.Host.ToString();
            return AjaxResult(_business.Login(dto), x =>
            {
                if (x.Split(".").Length == 3)
                    return Framework.Extension.StatusCode.OK;
                return Framework.Extension.StatusCode.Internal_Server_Error;
            });
        }

        [HttpPost("/openapi/v1/1.0.1/user/register")]
        public BaseResponse Register([FromBody] RegisterDtoIn dto)
        {
            return AjaxResult(_business.Register(dto), x =>
            {
                if (x.id > 0)
                    return Framework.Extension.StatusCode.OK;
                return Framework.Extension.StatusCode.Internal_Server_Error;
            });
        }

        [HttpPost("/openapi/v1/1.0.1/user/sendcode")]
        public BaseResponse SendCode([FromBody] SendCodeDtoIn dto)
        {
            return AjaxResult(_business.SendCode(dto), x =>
            {
                if (x.tel.Length == 11)
                    return Framework.Extension.StatusCode.OK;
                return Framework.Extension.StatusCode.Internal_Server_Error;
            });
        }

        [HttpGet("/openapi/v1/1.0.1/user/getinfo")]
        public BaseResponse GetInfo()
        {
            return OK(_permissionService
            .SetRules(LoginInfo.uid)
            .ValidatePermission<IUserBusiness>(new List<PermissionContent>() { new PermissionContent() { Content = "/openapi/v1/1.0.1/user/info", Type = Framework.CustomType.PermissionType.接口访问权限 } })
            .GetInfo(LoginInfo.name, LoginInfo.tel));
        }
    }
}
